import { error } from '../ui-components'

const NOT_FOUND = [
  "4040502",
  "4040504",
  "4040600",
  "4040700",
  "4040601",
]

export default () => next => action => {
	const { payload } = action

  if (payload === 'PASS') return;

	if (!(payload instanceof Error)) {
		return next(action)
	} else {
    const isProduction = process.env.DEV === 'production';
		const { response={} } = payload
    let msg = ''

    if (response.status == 503) {
      msg = '操作过于频繁，请稍候再试'
    }

    if (response.code == 4000104) {
      location.href = '/u'
      return
    }

    if (~NOT_FOUND.indexOf(''+response.code)) {

      location.replace('#/404')
      return
    }

    if (~(response.msg || response.errorInfo || '').indexOf('开小差')) {
      if (isProduction) return;
    }

    if (/^4/.test(response.status)) return
    if ('4000004' == response.code) return error(response.errorInfo);

		error(response.msg || msg || '服务出错。。。')

    if (isProduction) return;
    console.log(payload);
	}
}
